// pages/store/store.js
let template = require('./../../template/tabbar.js');
const app = getApp();
import { queryStoreList } from "../../service/onetoone_service"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:0,
    pageSize:200,
    sotreList: [
   
    ],
    leftList: [],
    rightList: [],
    bottomHouseList: [],
    isBottomShow: false,
    isShowLoading: false,
    bigBackgroundImgUrl:'https://jyhky.ylyc.top/shopbigbg_icon.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    template.tabbar("tabBar", 3, this, 0, 0)//0表示第一个tabbar
    wx.hideTabBar();
    if(app.globalData.shopBackground){
      this.setData({
        bigBackgroundImgUrl:app.globalData.shopBackground
      })
    }
    // this.queryStoreList();
  },

  

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     this.setData({
       page:0,
     })
     this.queryStoreList();
  },
  // 获取列表
  queryStoreList() {
    let pageNum = this.data.page;
    let pageSize = this.data.pageSize;
    wx.showLoading();
    queryStoreList(pageNum,pageSize).then(res => {
      console.log("JJLog:商城列表数据:" + JSON.stringify(res));
      wx.hideLoading();
      if(res.code == 200){
        let rightArr = this.data.rightList || [];
        let leftArr = this.data.leftList || [];
        let tempArr = this.data.bottomHouseList || [];
        let resultList = res.data.data || [];
        if (rightArr.length > 0 && pageNum > 0) {
          //加载下一页:
          let leftList = this.separationArr(resultList, 0);
          let rightList = this.separationArr(resultList, 1);
          tempArr = tempArr.concat(resultList);
          rightArr = rightArr.concat(rightList);
          leftArr = leftArr.concat(leftList);
          setTimeout(() => {
            if (resultList.length < pageSize) {
              this.setData({
                isShowLoading: false,
                isBottomShow: true,
              })
              this.setData({
                bottomHouseList: tempArr,
                leftList: leftArr,
                rightList: rightArr
              })
            } else if (resultList.length == pageSize) {
              this.setData({
                isShowLoading: false,
              })
              this.setData({
                bottomHouseList: tempArr,
                leftList: leftArr,
                rightList: rightArr
              })
            }
          }, 200)
        } else {
          this.setData({
            isShowLoading: false,
          })
          // console.log("JJLog:房源数据是===:" + JSON.stringify(res.data));
          let leftList = this.separationArr(resultList, 0);
          let rightList = this.separationArr(resultList, 1);
          setTimeout(() => {
            this.setData({
              leftList: leftList,
              rightList: rightList,
              bottomHouseList: resultList,
            })
          }, 300);

          if (resultList.length < pageSize) {
            this.setData({
              isBottomShow: true
            })
          }
        }
      }
    }).catch(err => {
      wx.hideLoading();
      
      let page = this.data.page;
      if (page > 0) {
        page = page - 1;
      }
      this.setData({
        isShowLoading: false,
        page: page
      })
    })
  },

  //分配数据:
  separationArr(arr, index) {
    //index 为 0 就为偶数项， 1:就是奇数项
    let tempArr = [];
    if (index == 0) {
      arr.forEach((item, index) => {
        if (index % 2 == 0) {
          tempArr.push(item);
        }

      })
    } else if (index == 1) {
      arr.forEach((item, index) => {
        if (index % 2 != 0) {
          tempArr.push(item);
        }

      })
    }
    return tempArr;
  },
  // 跳转到商品详情
  commonItemClick(e){
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/shopMallPackage/pages/newStoreDetail/newStoreDetail?id=' + id,
    })
  },



})